package com.zhangyanqiang.rabbitmqdemo.rest;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author zhangyanqiang
 * @date 2020/4/19
 **/
@RestController
@RequestMapping("/sendMessage")
public class SendMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RabbitAdmin rabbitAdmin;


    @RequestMapping(value = "/direct",method = RequestMethod.GET)
    public void sendDirectMessage(){

        String content = "hello this is a test message ";
        Map<String,Object> map = makeMessage();
        map.put("content",content);
        System.out.println("开始发送。。"+map.toString());
        rabbitTemplate.convertAndSend("testExchange","testDirectRoutKey",map);
    }

    @RequestMapping(value = "/deadLetter/{num}",method = RequestMethod.GET)
    public void sendTTlMessage(@PathVariable Integer num){

        String content = "hello this is a ttl message ";
        Map<String,Object> map = makeMessage();
        map.put("content",content);
        MessageProperties properties = new MessageProperties();
        properties.setExpiration(String.valueOf(num));
        Message message = new Message(map.toString().getBytes(),properties);

        System.out.println("开始发送ttl message。。"+map.toString());
        rabbitTemplate.convertAndSend("testExchange","ttlQueueKey",message);
    }

    @RequestMapping(value = "/exclusiveTrue1",method = RequestMethod.GET)
    public void sendExclusiveTrue1Message(){

        String content = "hello this is a test message ";
        Map<String,Object> map = makeMessage();
        map.put("content",content);
        System.out.println("开始发送。。"+map.toString());
        rabbitTemplate.convertAndSend("testExchange","exclusiveTrueQueueRoutKey",map);
    }

    private Map<String,Object> makeMessage(){
        String messageId = UUID.randomUUID().toString();
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("createTime",createTime);
        return map;
    }

    @RequestMapping(value = "/dynamicMessage/{num}")
    public String sendDynamicMqMessage(@PathVariable String num){

        Map<String,Object> dynamicQueueMap = new HashMap<>();
        dynamicQueueMap.put("x-dead-letter-exchange","testExchange");
        dynamicQueueMap.put("x-dead-letter-routing-key","receiveTTLQueueRoutKey");
        dynamicQueueMap.put("x-message-ttl",Integer.valueOf(num));
        Queue queue = new  Queue("dynamicQueue"+num,true,false,false,dynamicQueueMap);
        rabbitAdmin.declareQueue(queue);

        Binding binding = new Binding("dynamicQueue"+num,Binding.DestinationType.QUEUE,"testExchange","dynamicQueueRoutkey_"+num,null);
        rabbitAdmin.declareBinding(binding);

        String content = "hello this is a ttl message "+num;
        Map<String,Object> map = makeMessage();
        map.put("content",content);

        System.out.println("sending message :"+content+" "+ Calendar.getInstance().getTime());
        rabbitTemplate.convertAndSend("testExchange","dynamicQueueRoutkey_"+num,map);


        return num;
    }

    @RequestMapping(value = "/limitQueue",method = RequestMethod.GET)
    public void sendMessageForLimitQueue(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-overflow","drop-head");
        args.put("x-max-length",5);
        //声明数量受限的队列
        Queue queue = new  Queue("limitQueue",true,false,false,args);
        rabbitAdmin.declareQueue(queue);

        //绑定到交换机
        Binding binding = new Binding("limitQueue",Binding.DestinationType.QUEUE,"testExchange","limitQueueRoutkey",null);
        rabbitAdmin.declareBinding(binding);
        String content = "hello this is a test message ";
        Map<String,Object> map = makeMessage();
        map.put("content",content);
        System.out.println("开始发送。。"+map.toString());
        rabbitTemplate.convertAndSend("testExchange","limitQueueRoutkey",map);
    }


}
